<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: User.php 197 2011-02-18 12:45:33Z beimuaihui $
 */

//class Permission extends Baogg_Db_Table {
class QaField extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name;
	protected $_db;
	protected $_primary;
	
	function __construct() {
		$this->_name = 'qa_field';
		
		$this->primary = $this->_primary = 'qa_field_id';
		parent::__construct ();
	}
	
	public function genFilterByReport($report_id) {
		$where = array ();
		
		$arr_field = $this->getArray ( array ('qa_report_id' => ( int ) $report_id ) );
		$QaFilter = new QaFilter ();
		$rs_qa_filter = $QaFilter->getList ( array ('qa_report_id' => ( int ) $report_id, 'state' => 1 ) );
		foreach ( $rs_qa_filter as $v ) {
			$where [] = $this->genFilter ( $v, $arr_field );
		}
		return $where;
	}
	
	/**
	 * 
	 * gen sql_where by filter row
	 * @param filter row array $v
	 * @param report's all field $arr_field
	 */
	public function genFilter($v) {
		
		//echo __FILE__.__LINE__."<pre>";print_r($v);
		$field_name = $v ['qa_field_code'];
		
		//if value is empty and op is not abount null,then skip it.
		if (($v ['values'] === null || $v ['values'] === "") && ! in_array ( $v ['operation'], array ('is null', 'is not null' ) )) {
			return "";
		}
		
		if (in_array ( $v ['operation'], array ('in', 'not in' ) )) {
			if (is_string ( $v ['values'] )) {
				$v ['values'] = explode ( ',', $v ['values'] );
			}
			$where = "{$field_name} {$v['operation']} ('" . implode ( "','", $v ['values'] ) . "')";
		} else if ($v ['operation'] == 'between') {
			$arr_value = explode ( '~', $v ['values'] );
			$where = "{$field_name} {$v['operation']} '{$arr_value[0]}' and '{$arr_value[1]}')";
		} else if ($v ['operation'] == 'like') {
			$where = "{$field_name} {$v['operation']} '%{$v['values']}%' ";
		} else if ($v ['operation'] == 'is null' || $v ['operation'] == 'is not null') {
			$where = "{$field_name} {$v['operation']}  ";
		} else {
			$where = "{$field_name} {$v['operation']} '{$v['values']}' ";
		}
		return $where;
	}
	
	public function genOrderByReport($report_id) {
		$arr_order = array ();
		
		$arr_field = $this->getArray ( array ('qa_report_id' => ( int ) $report_id ) );
		$QaSort = new QaSort ();
		$rs_qa_filter = $QaSort->getList ( array ('qa_report_id' => ( int ) $report_id ), array ('sort' => 'ordering', 'dir' => 'asc' ) );
		foreach ( $rs_qa_filter as $v ) {
			//w.yecho __FILE__.__LINE__."<pre>";print_r()
			$field_name = $arr_field [$v ['qa_field_id']] ['qa_field_code'];
			$arr_order [] = "{$field_name} {$v['dir']}";
		}
		return $arr_order;
	}
	
	public function refreshState($qa_report_id = 0) {
		
		$QaList = new QaList ();
		$QaChart = new QaChart ();
		$QaFilter = new QaFilter ();
		$QaSort = new QaSort ();
		
		$rs = $this->getList ( array ('qa_report_id' => $qa_report_id ) );
		foreach ( ( array ) $rs as $v ) {
			//is_list=1 and not exist QaList
			if ($v ['is_list']) {
				$rs_list = $QaList->getList ( array ('qa_field_id' => $v ['qa_field_id'] ) );
				if (! $rs_list) {
					$this->_update ( array ('is_list' => 0 ), $v ['qa_field_id'] );
				}
			}
			//is_chart=1 and not exist QaChart
			if ($v ['is_chart']) {
				$rs_chart = $QaChart->getList ( array ('qa_field_id' => $v ['qa_field_id'] ) );
				if (! $rs_chart) {
					$this->_update ( array ('is_chart' => 0 ), $v ['qa_field_id'] );
				}
			}
			//is_filter=1 and not exist QaFilter
			if ($v ['is_filter']) {
				$rs_filter = $QaFilter->getList ( array ('qa_field_id' => $v ['qa_field_id'] ) );
				if (! $rs_filter) {
					$this->_update ( array ('is_filter' => 0 ), $v ['qa_field_id'] );
				}
			}
			//is_sort=1 and not exist QaSort
			if ($v ['is_sort']) {
				$rs_sort = $QaSort->getList ( array ('qa_field_id' => $v ['qa_field_id'] ) );
				if (! $rs_sort) {
					$this->_update ( array ('is_sort' => 0 ), $v ['qa_field_id'] );
				}
			}
		
		}
	}
	
	public function genFilterTextByFilterID($qa_field_id, $arr_field_map = array(), $qtypes = array()) {
		
		$Qa_Questions = new Qa_Questions ();
		
		$QaField = new QaField ();
		$rs_field = $QaField->getOne ( $qa_field_id );
		$field_name = $rs_field [0] ['qa_field_code'];
		
		$ret = array ();
		$Dict=new Dict();
		if ($rs_field [0] ['dict_id']) {
			$arr_combo = $Dict->getDictCombo (  $rs_field [0] ['dict_id'] );
			foreach((array)$arr_combo as $v){
				$ret[$v[0]]=$v[1];
			}
		} else if(!in_array($field_name,array('id','submitdate'))) {
			/* 	
 */
		/* 	if (in_array ( $rs_field [0] ['qa_field_code'], array ('id', 'submitdate' ) )) {
				$output .= '<td>' . $rs_field [0] ['qa_field_name'] . ":</td>";
				$output .= '<td>' . Qa_Questions::genOp ( $field_name ) . '</td>';
				$output .= "<td><input type='text' name='field_value[$field_name]' value='' />  </td>";
				return $output;
			} */
			
			$QaSurveys = new QaSurveys ();
			$rs_qasurveys = $QaSurveys->getOne ( @$rs_field [0] ['qa_surveys_id'] );
			$Qa_Surveys = new Qa_Surveys ();
			$rs_qa_surveys = $Qa_Surveys->getOne ( $rs_qasurveys [0] ['sid'] );
			$language = $rs_qa_surveys [0] ['language'];
			
			if (! $arr_field_map) {
				$arr_field_name=explode('X',$field_name);
				
				//get question group's sub question, is question id if prefix field_code,then gen fieldmap and compare field_code
				$rs_tmp_field=$Qa_Questions->getList(array('sid'=>$arr_field_name[0],'gid'=>$arr_field_name[1]));
				foreach((array)$rs_tmp_field as $v_tmp_field){
					if (strpos($arr_field_name[2],$v_tmp_field['qid']) === 0){
						$arr_tmp_field_map = $Qa_Questions->genFieldMapByQuestion($v_tmp_field['qid']);
						if(isset($arr_tmp_field_map[$field_name])){
							$arr_field_map=$arr_tmp_field_map;
							break;
						}
					}
				}
				
				$arr_field_map = $arr_field_map?$arr_field_map:$Qa_Questions->genFieldMap ( @$rs_qasurveys [0] ['sid'] );
			}
			if (! $qtypes) {
				Zend_Loader::loadFile ( 'question_attribute.php', 'config/qa/', true );
				$qtypes = getqtypelist ();
			}
			
			$type = $arr_field_map [$field_name] ['type'];
			$qid = $arr_field_map [$field_name] ['qid'];
			$arr_answer = array ();
			$Qa_Answers = new Qa_Answers ();
			
			if ($qtypes [$type] ['answerscales'] != 0) {
				$scale_id = isset ( $arr_field_map [$field_name] ['scale_id'] ) ? $arr_field_map [$field_name] ['scale_id'] : 0;
				if (! isset ( $arr_answer [$qid] )) {
					$rs_sub_answer = $Qa_Answers->getList ( array ('qid' => ( int ) $qid, 'language' => $language ), array ('sort' => 'sortorder', 'dir' => 'asc' ) );
					foreach ( $rs_sub_answer as $v_sub_answer ) {
						$arr_answer [$qid] [$v_sub_answer ['scale_id']] [$v_sub_answer ['code']] = $v_sub_answer ['answer'];
					}
				}
				//isset($arr_answer[$qid][$val]) && $rs[$k][$field_name]=$arr_answer[$qid][$val];
				

			/* 	$output .= '<td>' . $rs_field [0] ['qa_field_name'] . ":</td>";
				$output .= '<td>' . Qa_Questions::genOp ( $field_name, '', 'multi' ) . '</td>';
				$output .= "<td><select name='field_value[{$field_name}][]' multiple='multiple'>"; */
				foreach ( ( array ) $arr_answer [$qid] [$scale_id] as $v_code => $v_answer ) {
					//$str_selected = ''; //in_array($v_code,$arr_field_value_text)?'  selected="selected" ':'';
					//$output .= "<option value=\"{$v_code}\" {$str_selected}>({$v_code})" . strip_tags ( $v_answer ) . "</option>";
					$ret[$v_code]=strip_tags ( $v_answer );
				}
				//$output .= "</select>  </td>";
			
			} else if (in_array ( $type, array ('G', 'Y', 'C', 'E' ) )) {
				$arr_value = array ();
				$arr_value ['G'] = array ('F', 'M' );
				$arr_value ['Y'] = array ('Y', 'N' );
				$arr_value ['C'] = array ('Y', 'U', 'N' );
				$arr_value ['E'] = array ('I', 'S', 'D' );
				
				foreach($arr_value [$type]  as $v){
					$ret[$v]=Baogg_Language::get($v);
				}
				/* $arr_label = array_map ( array ('BAOGG_LANGUAGE', 'get' ), $arr_value [$type] );
				$arr_option = array_fill ( 0, count ( $arr_value [$type] ), '<option value="%s" %s>%s</option>' );
				$arr_selected = array_fill ( 0, count ( $arr_value [$type] ), '' );
				
				 $arr_selected_only=	array_intersect($arr_value[$type], $arr_field_value_text);
			($selected_only_cnt=count($arr_selected_only)) && ($arr_selected+=array_combine(array_keys($arr_selected_only),array_fill(0,$selected_only_cnt,' selected="selected" ')));
	 
				
				$arr_option = array_map ( "sprintf", $arr_option, $arr_value [$type], $arr_selected, $arr_label );
				
				$output .= '<td>' . $rs_field [0] ['qa_field_name'] . ":</td>";
				$output .= '<td>' . Qa_Questions::genOp ( $field_name, '', 'multi' ) . '</td>';
				$output .= "<td><select name=\"field_value[$field_name][]\" multiple='multiple'>" . implode ( "", $arr_option ) . "</select></td>";
			 */
		/* 	} else if (in_array ( $type, array ('5', 'B' ) )) {
				$arr_value = array ();
				$arr_value ['5'] = range ( 1, 5 );
				$arr_value ['B'] = range ( 1, 10 );
				
				
				foreach($arr_value[$type] as $v){
					$ret[$v]=$v;
				} */
				/* $arr_label = $arr_value [$type];
				$arr_option = array_fill ( 0, count ( $arr_value [$type] ), '<option value="%d" %s>%d</option>' );
				$arr_selected = array_fill ( 0, count ( $arr_value [$type] ), '' );
				
				 $arr_selected_only=	array_intersect($arr_value[$type], $arr_field_value_text);
			($selected_only_cnt=count($arr_selected_only)) && ($arr_selected+=array_combine(array_keys($arr_selected_only),array_fill(0,$selected_only_cnt,' selected="selected" ')));
	 
				$arr_option = array_map ( "sprintf", $arr_option, $arr_value [$type], $arr_selected, $arr_label );
				
				$output .= '<td>' . $rs_field [0] ['qa_field_name'] . ":</td>";
				$output .= '<td>' . Qa_Questions::genOp ( $field_name, $rs_filter [0] ['operation'], 'multi' ) . '</td>';
				$output .= "<td><select name=\"field_value[$field_name][]\" multiple='multiple'>" . implode ( "", $arr_option ) . "</select></td>";
			 */
			} else  {
				
				/* //TODO
				$output .= '<td>' . $rs_field [0] ['qa_field_name'] . ":</td>";
				$output .= '<td>' . Qa_Questions::genOp ( $field_name, '', 'check' ) . '</td>';
				
				$rs_qa_questions = $Qa_Questions->getList ( array ("parent_qid" => $qid, 'title' => @$arr_field_map [$field_name] ['aid'] ) );
				if ($rs_qa_questions) {
					//$str_checked=$field_value_text=='Y'?'checked="checked"':'';
					$output .= '<td><input type="checkbox" name="field_value[' . $field_name . ']"  ' . $str_checked . '  value="Y"  /></td>';
				} else {
					$output .= "<td><input type=\"text\" name=\"field_value[{$field_name}]\" value=\"\" value=\"Y\" /></td>";
				}
			} else {
				$output .= '<td>' . $rs_field [0] ['qa_field_name'] . ":</td>";
				$output .= '<td>' . Qa_Questions::genOp ( $field_name ) . '</td>';
				$output .= "<td><input type='text' name='field_value[$field_name]' value='' />  </td>"; */
			}
		}
		
	
		return $ret;
	
	}
	
	public function addComboAll2($arr){
		array_unshift($arr,array('#ALL#',Baogg_Language::get('select_all')));
		return $arr;
	}

}